package com.test.mooan.labama.view_boom.widget_boom;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;

import java.util.ArrayList;

public class BoomEditText extends AppCompatEditText implements TextWatcher, View.OnFocusChangeListener {

    private long focusTime = 0;
    private long outOfFocusTime = 0;

    private boolean hasPaste = false;
    private boolean hasEdit = false;

    private boolean pasting = false;

    private String eventWord = "";


    private final ArrayList<OnFocusChangeListener> onFocusChangeListenerList = new ArrayList<>();

    public BoomEditText(@NonNull Context context) {
        super(context);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    public BoomEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    public BoomEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    @Override
    public boolean onTextContextMenuItem(int id) {
        if (id == android.R.id.paste) {
            pasting = true;
            hasPaste = true;
        }
        return super.onTextContextMenuItem(id);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}
    @Override
    public void afterTextChanged(Editable s) {
        if (pasting) pasting = false;
        else hasEdit = true;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        if (hasFocus) {
            focusTime = System.currentTimeMillis();
        }
        else {
            outOfFocusTime = System.currentTimeMillis();
            long time = outOfFocusTime - focusTime;
            Editable input = getText();
            String content = input == null ? "":input.toString();
            String remark = content+"$$"+time+"$$"+(hasPaste ? "P":"")+(hasEdit ? "E":"");
            UserInfoHelper_boom.gInstance().addEventPoint(eventWord,remark);
        }


        for (OnFocusChangeListener listener : onFocusChangeListenerList){
            listener.onFocusChange(v, hasFocus);
        }

    }

    public void stEventWord(String eventWord) {
        this.eventWord = eventWord;
    }

    public String gtTextString(){

        Editable editable = getText();
        if (editable == null) return "";
        return Stringutil_boom.getSafeString(editable.toString());
    }


    public void addOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener){
        if (onFocusChangeListener!=null) onFocusChangeListenerList.add(onFocusChangeListener);
    }


}
